#include "AgentServer.hpp"
#include "EventHandlerAgentConnection.hpp"
#include "cybertron/core/UtilTime.hpp"
#include "cybertron/core/Thread.hpp"
#include "TheApp.hpp"
//#include "cybertron/core/UtilUrlRequest.hpp"
#pragma warning(disable:4996)
AgentServer::AgentServer()
{
	mListenPort = 0;
	UtilTime::millisecondsUpdate();
	zeroMembers();
}

AgentServer::~AgentServer()
{
	clearThis();
}
void AgentServer::SetListenPort(int ListenPort) {
	mListenPort = ListenPort;
}

//void AgentServer::TaskStartEvent(std::string task_id) {
//	//if (mSonServerInfo.EnableSONServer) {
//	//	SimOneEnv::SimOneCloudEnvInfo envInfo;
//	//	SimOneEnv::GetInstance().GetCloundEnvInfo(envInfo);
//
//	//	theApp->getNodeSONClient()->sendSetStateMessage(TaskRunnerPool::ESONState::ESONStateDoing);
//
//	//	std::string error;
//	//	bool succeed = false;
//
//	//	std::string url = "http://" + mSonServerInfo.SONServerAddr + ":" + std::to_string(mSonServerInfo.SONHttpServerAddrPort) + "/api/v1/node/report?";
//	//	std::string param = "command=pod-task-map&";
//	//	param += "pod-name=" + envInfo.PodName + "&";
//	//	param += "task-id=" + task_id + "&";
//	//	param += "type=" + mType + "&";
//	//	param += "groupId=" + mGroupId + "&";
//	//	param += "id=" + mId;
//
//	//	url += param;
//
//	//	try
//	//	{
//	//		string result = UtilUrlRequest::GetUrl(url);
//	//		succeed = true;
//	//		logInfo("pod-task-map successful:%s", url.c_str());
//	//	}
//	//	catch (const UtilUrlRequest::TException& ex)
//	//	{
//	//		error = ex.what();
//	//		succeed = false;
//	//		logInfo("pod-task-map error:%s", url.c_str());
//	//	}
//
//	//}
//}
//void AgentServer::TaskEndEvent(std::string task_id) {
//	if (mSonServerInfo.EnableSONServer) {
//		theApp->getNodeSONClient()->sendSetStateMessage(TaskRunnerPool::ESONState::ESONStateDone);
//	}
//	
//}
std::string AgentServer::getId() {
	//getpid()
	//std::thread::id id = std::this_thread::get_id();
	//
	//stringstream ss;
	//ss << id;
	//string mystring = ss.str();
	string mystring = "";
	return mystring;
}
void AgentServer::run()
{
	//SimOneEnv::GetInstance().GetSONServerInfo(mSonServerInfo);
	

	UtilTime::millisecondsUpdate();
	int t0 = UtilTime::millisecondsCurrent();

	for (;;)
	{
		UtilTime::millisecondsUpdate();
		int t1 = UtilTime::millisecondsCurrent();
		int ms = t1 - t0;
		t0 = t1;

		//theApp->getWorkSessionManager()->onTimer(ms);

		std::this_thread::sleep_for(std::chrono::milliseconds(200));
	}
}

void AgentServer::onAccept(SocketBasePtr pRemoteSocket)
{
	if (pRemoteSocket == nullptr)
	{
		return;
	}

	std::shared_ptr<EventHandlerAgentConnection> pHandler = 
		std::make_shared<EventHandlerAgentConnection>();
	pRemoteSocket->addHandler(pHandler);
}

void AgentServer::zeroMembers()
{
	//mGroupId = "Coordinator";
	//mType = std::to_string(TaskRunnerPool::ESONType::ECybertronCoordinator);;
	mId = getId();
}

void AgentServer::clearThis()
{

	
}
